package com.oneinlet.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;


/**
 * Created by WangZiHe on 2018/6/12
 * QQ/WeChat:648830605
 * QQ-Group:368512253
 * Blog:www.520code.net
 * Github:https://github.com/yancheng199287
 */
@Configuration
public class CaffeineCacheConfig {

    @Bean
    public CacheManager caffeineCacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder();
        cacheBuilder.initialCapacity(10)
                .maximumSize(100)
                .weakKeys()
                .softValues()
                .refreshAfterWrite(120, TimeUnit.SECONDS)//距离上一次写入的刷新时间
                .expireAfterWrite(10, TimeUnit.MINUTES)//距离上一次写入的过期时间
                .expireAfterAccess(30, TimeUnit.MINUTES)//距离上一次访问的过期时间
                .executor(Runnable::run);
        cacheManager.setCaffeine(cacheBuilder);
        return cacheManager;
    }

}
